

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pymunk.pygame_util &mdash; pymunk 3.0.0 documentation</title>
    
    <link rel="stylesheet" href="../../_static/pymunk.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '3.0.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="shortcut icon" href="../../_static/pymunk_favicon.ico"/>
    <link rel="top" title="pymunk 3.0.0 documentation" href="../../index.html" />
    <link rel="up" title="pymunk" href="../pymunk.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">pymunk 3.0.0 documentation</a> &raquo;</li>
          <li><a href="../index.html" >Module code</a> &raquo;</li>
          <li><a href="../pymunk.html" accesskey="U">pymunk</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for pymunk.pygame_util</h1><pre>
# ----------------------------------------------------------------------------
# pymunk
# Copyright (c) 2007-2012 Victor Blomqvist
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ----------------------------------------------------------------------------

"""Contains helper functions to help using pygame with pymunk.

Intended to help with debugging and prototyping, not for actual production use
in a full application. The methods contained in this module is opinionated 
about your coordinate system and not in any way optimized. 
"""

__version__ = "$Id$"
__docformat__ = "reStructuredText"

__all__ = ["draw_space", "draw_shape", "draw_circle", "draw_poly"
         , "draw_segment", "draw_constraint", "to_pygame", "from_pygame"]

import pygame
from pygame.locals import *
from pygame.color import *

import pymunk
from pymunk.vec2d import Vec2d



<div class="viewcode-block" id="draw_space"><a class="viewcode-back" href="../../pymunk.pygame_util.html#pymunk.pygame_util.draw_space">[docs]</a>def draw_space(surface, space):
    """Draw the contents of a pymunk.Space object on a pygame.Surface object
    
    This method currently supports drawing of
        * pymunk.Segment
        * pymunk.Circle
        * pymunk.Poly
        * pymunk.Constraint objects

    You can control the color of a shape by setting shape.color to the color 
    you want it drawn in.
    
    &gt;&gt;&gt; my_shape.color = pygame.color.THECOLORS["pink"]
        
    Not all constraints are currently drawn in a very clear way, but all the 
    different shapes should look fine both as static and dynamic objects.
    
    See pygame_util.demo.py for a full example
    
    :Parameters:
            surface : pygame.Surface
                Surface that the space will be drawn on
            space : pymunk.Space
                The contents of this Space will be drawn on the surface. 
    """
    
    (width, height) = surface.get_size()
    
    for s in space.shapes:
        draw_shape(surface, s)
            
    for c in space.constraints:
        draw_constraint(surface, c)
</div>
<div class="viewcode-block" id="draw_shape"><a class="viewcode-back" href="../../pymunk.pygame_util.html#pymunk.pygame_util.draw_shape">[docs]</a>def draw_shape(surface, shape):
    """Draw a pymunk.Shape object
    
    See the documentation of draw_space for full details
    
    :Parameters:
            surface : pygame.Surface
                Surface that the space will be drawn on
            shape : pymunk.Shape
                The Shape object to draw
    """
    if isinstance(shape, pymunk.Circle):
        draw_circle(surface, shape)
    elif isinstance(shape, pymunk.Segment):
        draw_segment(surface, shape)
    elif  isinstance(shape, pymunk.Poly):
        draw_poly(surface, shape)
        </div>
<div class="viewcode-block" id="draw_circle"><a class="viewcode-back" href="../../pymunk.pygame_util.html#pymunk.pygame_util.draw_circle">[docs]</a>def draw_circle(surface, circle):
    """Draw a pymunk.Circle object
    
    See help of draw_space for full details
    
    :Parameters:
            surface : pygame.Surface
                Surface that the space will be drawn on
            shape : pymunk.Circle
                The circle shape to draw
    """
    circle_center = circle.body.position + circle.offset
    p = to_pygame(circle_center, surface)
    r = 0
    if hasattr(circle, "color"):
        color = circle.color  
    elif circle.body.is_static:
        color = THECOLORS["lightgrey"]
        r = 1
    else:
        color = THECOLORS["red"]
    pygame.draw.circle(surface, color, p, int(circle.radius), r)
    
    circle_edge = circle_center + Vec2d(circle.radius, 0).rotated(circle.body.angle)
    p2 = to_pygame(circle_edge, surface)
    line_r = 3 if circle.radius &gt; 20 else 1
    pygame.draw.lines(surface, THECOLORS["blue"], False, [p,p2], line_r)
</div>
<div class="viewcode-block" id="draw_poly"><a class="viewcode-back" href="../../pymunk.pygame_util.html#pymunk.pygame_util.draw_poly">[docs]</a>def draw_poly(surface, poly):
    """Draw a pymunk.Poly object
    
    See help of draw_space for full details
    
    :Parameters:
            surface : pygame.Surface
                Surface that the space will be drawn on
            shape : pymunk.Poly
                The poly shape to draw
    """
    ps = poly.get_points()
    ps = [to_pygame(p, surface) for p in ps]
    ps += [ps[0]]
    if hasattr(poly, "color"):
        color = poly.color  
    elif poly.body.is_static:
        color = THECOLORS["lightgrey"]
    else:
        color = THECOLORS["green"]
    pygame.draw.lines(surface, color, False, ps, 1)
</div>
<div class="viewcode-block" id="draw_segment"><a class="viewcode-back" href="../../pymunk.pygame_util.html#pymunk.pygame_util.draw_segment">[docs]</a>def draw_segment(surface, segment):
    """Draw a pymunk.Segment object
    
    See help of draw_space for full details
    
    :Parameters:
            surface : pygame.Surface
                Surface that the space will be drawn on
            shape : pymunk.Segment
                The segment shape to draw
    """
    body = segment.body
    pv1 = body.position + segment.a.rotated(body.angle)
    pv2 = body.position + segment.b.rotated(body.angle)

    p1 = to_pygame(pv1, surface)
    p2 = to_pygame(pv2, surface)
    
    if hasattr(segment, "color"):
        color = segment.color  
    elif segment.body.is_static:
        color = THECOLORS["lightgrey"]
    else:
        color = THECOLORS["blue"]
    pygame.draw.lines(surface, color, False, [p1,p2], max(int(segment.radius),1))
    </div>
<div class="viewcode-block" id="draw_constraint"><a class="viewcode-back" href="../../pymunk.pygame_util.html#pymunk.pygame_util.draw_constraint">[docs]</a>def draw_constraint(surface, constraint):
    """Draw a pymunk.Constraint object
    
    See the documentation of draw_space for full details
    
    :Parameters:
            surface : pygame.Surface
                Surface that the space will be drawn on
            shape : pymunk.Shape
                The Shape object to draw
    """
    if isinstance(constraint, pymunk.GrooveJoint) and hasattr(constraint, "groove_a"):
        pv1 = constraint.a.position + constraint.groove_a
        pv2 = constraint.a.position + constraint.groove_b
        p1 = to_pygame(pv1, surface)
        p2 = to_pygame(pv2, surface)
        pygame.draw.aalines(surface, THECOLORS["darkgray"], False, [p1,p2])
    elif isinstance(constraint, pymunk.PinJoint):
        pv1 = constraint.a.position + constraint.anchr1.rotated(constraint.a.angle)
        pv2 = constraint.b.position + constraint.anchr2.rotated(constraint.b.angle)
        p1 = to_pygame(pv1, surface)
        p2 = to_pygame(pv2, surface)
        pygame.draw.aalines(surface, THECOLORS["darkgray"], False, [p1,p2])    
    elif isinstance(constraint, pymunk.GearJoint):
        pv1 = constraint.a.position
        pv2 = constraint.a.position
        p1 = to_pygame(pv1, surface)
        p2 = to_pygame(pv2, surface)
        pygame.draw.circle(surface, THECOLORS["darkgray"], p1, 3)
        pygame.draw.circle(surface, THECOLORS["darkgray"], p2, 3)
    elif hasattr(constraint, "anchr1"):
        pv1 = constraint.a.position + constraint.anchr1.rotated(constraint.a.angle)
        pv2 = constraint.b.position + constraint.anchr2.rotated(constraint.b.angle)
        p1 = to_pygame(pv1, surface)
        p2 = to_pygame(pv2, surface)
        pygame.draw.aalines(surface, THECOLORS["darkgray"], False, [p1,p2])
    else:
        pv1 = constraint.a.position
        pv2 = constraint.b.position
        p1 = to_pygame(pv1, surface)
        p2 = to_pygame(pv2, surface)
        pygame.draw.aalines(surface, THECOLORS["darkgray"], False, [p1,p2])    
</div>
def get_mouse_pos(surface):
    """Get position of the mouse pointer in pymunk coordinates."""
    p = pygame.mouse.get_pos()
    return from_pygame(p, surface)

<div class="viewcode-block" id="to_pygame"><a class="viewcode-back" href="../../pymunk.pygame_util.html#pymunk.pygame_util.to_pygame">[docs]</a>def to_pygame(p, surface):
    """Convenience method to convert pymunk coordinates to pygame surface 
    local coordinates
    """
    return int(p[0]), surface.get_height()-int(p[1])
    </div>
<div class="viewcode-block" id="from_pygame"><a class="viewcode-back" href="../../pymunk.pygame_util.html#pymunk.pygame_util.from_pygame">[docs]</a>def from_pygame(p, surface):
    """Convenience method to convert pygame surface local coordinates to 
    pymunk coordinates    
    """
    return to_pygame(p,surface)

            </div>
</pre>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/pymunk_logo_sphinx.png" alt="Logo"/>
            </a></p>
<h3><a href="../../index.html">Table Of Contents</a></h3>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../readme.html">Readme</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../installation.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../pymunk.html">API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../examples.html">Examples</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../tutorials.html">Tutorials</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../advanced.html">Advanced</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">pymunk 3.0.0 documentation</a> &raquo;</li>
          <li><a href="../index.html" >Module code</a> &raquo;</li>
          <li><a href="../pymunk.html" >pymunk</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Victor Blomqvist.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>